package ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.NoSuchEntityException;
import javax.sql.DataSource;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import ejb.LookupHelper.LookupHelper;
public class ObjectsBean implements EntityBean {

private static final String DATA_SOURCE_NAME = "jdbc/shaker";
EntityContext entityContext;
private Integer widgetId;
public int id;        // OBJECT_ID
public int parent_id;   // PARENT_ID
public int object_type_id; // OBJECT_TYPE_ID
public String name;   // NAME
public String description;   // DESCRIPTION
public int order_number; //ORDER_NUMBER
// Implementation for all the Remote Interface business methods
public int getId () throws RemoteException {
  return id;
 }
public int getParentId () throws RemoteException {
  return parent_id;
 }
public String getName () throws RemoteException {
  return name;
 }
public int getObjectTypeId () throws RemoteException {
  return object_type_id;
 }
public int getOrderNumber () throws RemoteException {
  return order_number;
 }
public String getDescription () throws RemoteException {
  return description;
 }
public void setDecription (String descr) throws RemoteException {
  this.description = descr;
 }
// Implementation for all the Home Interface methods
public void ejbCreate (int _id, int _parent_id, int _object_type_id,
        String _name, String _description,
            int _order_number )
  throws CreateException, RemoteException {
  this.id = _id;        // OBJECT_ID
  this.parent_id=_parent_id;   // PARENT_ID
  this.object_type_id=_object_type_id; // OBJECT_TYPE_ID
  this.name = _name;
  this.description=_description;
  this.order_number=_order_number;
}
public void ejbPostCreate (int isbn, String bookTitle, String author,
                               double cost, int disc) {
}
// Implement all the mandatory methods required by the EJB Spec
public void ejbActivate () throws RemoteException{}
public void ejbPassivate () throws RemoteException{}
public void ejbRemove () throws RemoteException, RemoveException{}
public void ejbLoad()
{
      widgetId =  (Integer)entityContext.getPrimaryKey();
      try
	{
	loadRow();
	}
      catch   (Exception ex)
	{
	throw new EJBException("ejbLoad:   " + ex.getMessage());
	}
}

public void ejbStore()
{
      try
	{
	storeRow();
	}
      catch   (Exception ex)
	{
	throw new EJBException("ejbLoad:   " + ex.getMessage());
	}
}
public void setEntityContext (EntityContext context)
  throws RemoteException {
  entityContext = context;
 }
public void unsetEntityContext () throws RemoteException {
  entityContext = null;
 }
private void loadRow()   throws SQLException, NamingException
	{
	Connection con = null;
        DataSource dataSource = LookupHelper.getInstance ().lookup (DATA_SOURCE_NAME, DataSource.class);
	PreparedStatement prepStmt = null;
	try
		{
		con = dataSource.getConnection();
		String selectStatement ="select object_id, parent_id, object_type_id, name, description,order_number " +
		"from objects where ID = ?  ";
		prepStmt = con.prepareStatement(selectStatement);
		prepStmt.setInt(1,  widgetId);
		ResultSet rs = prepStmt.executeQuery();
		if   (rs.next())
			{
                            this.parent_id = rs.getInt(2);
                            this.description = rs.getString(5);
                            this.id = rs.getInt(1);
                            this.name = rs.getString(4);
                            this.order_number = rs.getInt(6);
                            prepStmt.close();
			}
		else
			{
                            prepStmt.close();
                            throw new NoSuchEntityException("Row for widgetId " + widgetId + " not found in database.");
			}
		con.close();
		}
	catch   (Exception e)
		{
		}
	finally
		{
		if   (prepStmt   != null)
			{
			try
			{
			prepStmt.close();
			}
			catch   (Exception e)
			{
			}
			}
		if   (con   != null)
			{
			try
			{
			con.close();
			}
			catch   (Exception e)
			{
			}
			}
		}
	}

private void storeRow()   throws SQLException, NamingException
	{
	Connection con = null;
        DataSource dataSource = LookupHelper.getInstance ().lookup (DATA_SOURCE_NAME, DataSource.class);
	PreparedStatement prepStmt = null;
	try
		{
            //String selectStatement ="select object_id, parent_id, object_type_id, name, description,order_number " +
                    con = dataSource.getConnection();
                    String updateStatement = "update objects set parent_id = ? "
                            + "object_type_id = ? " + "name = ? " 
                            + "description = ? " + "order_number = ? "
                            + "where ID = ?";
                    prepStmt = con.prepareStatement(updateStatement);
                    prepStmt.setInt(1,  this.parent_id);
                    prepStmt.setInt(2,  this.object_type_id);
                    prepStmt.setString(3, this.name);
                    prepStmt.setString(4, this.description);
                    prepStmt.setInt(5, this.order_number);
                    prepStmt.setInt(6, widgetId);
                    int rowCount = prepStmt.executeUpdate();
                    prepStmt.close();
                    con.close();

		if   (rowCount == 0)
			{
			throw new EJBException("Storing row for widgetId " + widgetId + " failed.");
			}
		}
	catch   (Exception e)
		{
		}
	finally
		{
		if (prepStmt != null)
			{
			try
			{
			prepStmt.close();
			}
			catch   (Exception e)
			{
			}
		}
		if   (con   != null)
			{
			try
			{
			con.close();
			}
			catch   (Exception e)
			{
			}
			}
		}
	}
}
